package com.pg_chat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pg_chat.config.YmlProperties;
import com.pg_chat.model.Constant;
import com.pg_chat.model.UserEntity;
import com.pg_chat.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.apache.tomcat.util.security.MD5Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller("loginController")
@RequestMapping("")
public class LoginController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private UserService userService;

    @Resource
    private YmlProperties ymlProperties;



    @RequestMapping("/login")
    public ModelAndView loginPage(HttpSession session){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pages/login");
        if(!(session.getAttribute(Constant.GLOBAL_USER)==null))
            mv.setViewName("redirect:/index");
        return mv;
    }

    @PostMapping(path = "/login/verify")
    public ModelAndView checkLogin(String username, String password, HttpServletRequest request,
                                   HttpSession session){
        Boolean md5verify = ymlProperties.isMd5verify();
        //md5加密
        String md5Pwd = MD5Encoder.encode(password.getBytes());
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/login");
        mv.addObject("authState","true");

        if (md5verify && userService.verifyMD5(username,md5Pwd)){
            session.setAttribute(Constant.GLOBAL_USER
                    ,userService.userMsgByName(username));
            mv.setViewName("redirect:/index");
        }else if (!md5verify && userService.verify(username,password)){
            session.setAttribute(Constant.GLOBAL_USER
                    ,userService.userMsgByName(username));
            mv.setViewName("redirect:/index");
        }else{
            mv.addObject("authState","false");
        }

        return mv;
    }

    @RequestMapping("/signUp")
    public ModelAndView signUpPage(HttpServletRequest request){
        return new ModelAndView("/pages/");
    }

    @RequestMapping("/signUp/verify")
    public String register(@Param("user") UserEntity user){
        JSON jsonObject = new JSONObject();
        HashMap<String,String> msgMap = new HashMap<>();
        StringBuffer data = new StringBuffer();
        //设置返回体
        msgMap.put("result","false");

        //用户名是否已注册
        if(userService.userMsgByName(user.getUsername()) == null){
            data.append(userService.registerCheck(user));
            if(data.capacity() == 0)
                msgMap.put("result","ture");
        }
        msgMap.put("data",JSON.toJSONString(data.toString()));

        String jsonStr = jsonObject.toJSONString();
        return jsonStr;
    }

    @RequestMapping("/resetAccount")
    public ModelAndView resetAccount(HttpServletRequest request){

        return new ModelAndView("/pages/resetAccount");
    }


    public String test(){
        return "/pages/login";
    }
}
